﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;

namespace JqueryNet.FormControl.FormWebControl
{
    public class JWebControl : IJQuery
    {
        private WebControl webControl;

        public JWebControl(WebControl _control)
        {
            JControl = _control;
            this.webControl = _control;
        }
        protected List<KeyVal> NewObjectToAttr(object obj)
        {
            List<KeyVal> vals = new List<KeyVal>();
            PropertyInfo[] pinfos = obj.GetType().GetProperties();
            foreach (PropertyInfo item in pinfos)
            {
                vals.Add(new KeyVal() { Key = item.Name, Val = item.GetValue(obj, null) == null ? string.Empty : item.GetValue(obj, null).ToString() });
            }
            return vals;
        }
        #region IJQuery 成员

        public object Value{get;set;}

        public System.Web.UI.Control JControl{get;set;}

        public virtual void Attr(string key)
        {
            Value = webControl.Attributes["key"];
        }

        public virtual void Attr(string key, string value)
        {
            webControl.Attributes.Add(key, value);
        }

        public virtual void Attr(object newobject)
        {
            List<KeyVal> val = NewObjectToAttr(newobject);
            foreach (var item in val)
                Attr(item.Key, item.Val);
        }

        public virtual void Attr(string key, Func<string> value)
        {
            Attr(key, value());
        }

        public virtual void RemoveAttr(string key)
        {
            webControl.Attributes.Remove(key);
        }

        public virtual void AddClass(string cssclass)
        {
            webControl.Attributes.Add("class", cssclass);
        }

        public virtual void AddClass(Func<string> fun)
        {
            AddClass(fun());
        }

        public virtual void RemoveClass()
        {
            webControl.Attributes.Remove("class");
        }

        public virtual void ToggleClass(string cssclass)
        {
            string value = webControl.Attributes["class"];
            if (!string.IsNullOrEmpty(value))
                RemoveClass();
            else
                AddClass(cssclass);
        }

        public virtual void ToggleClass(Func<string> fun)
        {
            ToggleClass(fun());  
        }

        public virtual void Val()
        {
            Value = webControl.Attributes["value"];
        }

        public virtual void Val(string value)
        {
            webControl.Attributes["value"] = value;
        }

        public virtual void Val(Func<string> fun)
        {
            Val(fun());
        }

        public virtual void Val(string[] values)
        {
            if (values != null && values.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string item in values)
                {
                    sb.AppendFormat("{0},", item);
                }
                Val(sb.ToString().TrimEnd(','));
            }
        }

        public virtual void Children()
        {
            Value = webControl.Controls;
        }

        public virtual void Children<T>(Predicate<T> prc)
        {
            List<T> list = new List<T>();
            var s = from p in webControl.Controls.OfType<T>() select p;
            foreach (T item in s)
            {
                if (prc(item))
                    list.Add(item);
            }
            Value = list;
        }

        public virtual void Parent()
        {
            Value = webControl.Parent;
        }

        public virtual void Css(string key)
        {
            Value = webControl.Attributes.CssStyle[key];
        }

        public virtual void Css(string key, string value)
        {
            webControl.Attributes.CssStyle[key] = value;
        }

        public virtual void Css(object newobject)
        {
            List<KeyVal> list = NewObjectToAttr(newobject);
            foreach (KeyVal item in list)
            {
                Css(item.Key, item.Val);
            }
        }

        public virtual void Css(string key, Func<string> value)
        {
            Css(key, value());
        }

        public virtual void Height()
        {
            var hi = webControl.Attributes["height"];
            if (string.IsNullOrEmpty(hi))
            {
                hi = webControl.Attributes.CssStyle["height"];
            }
            Value = hi;
        }

        public virtual void Height(string value)
        {
            webControl.Attributes.CssStyle["height"] = value;
        }

        public virtual void Width()
        {
            var hi = webControl.Attributes["width"];
            if (string.IsNullOrEmpty(hi))
            {
                hi = webControl.Attributes.CssStyle["width"];
            }
            Value = hi;
        }

        public virtual void Width(string value)
        {
            webControl.Attributes.CssStyle["width"] = value;
        }

        public virtual void Bind(string key, string value)
        {
            Attr(key, value);
        }

        public virtual void UnBind(string key)
        {
            RemoveAttr(key);
        }

        public virtual void Blur(string fun)
        {
            Attr(ControlFactory.BLUR, fun);
        }

        public virtual void Change(string fun)
        {
            Attr(ControlFactory.CHANGE, fun);
        }

        public virtual void Click(string fun)
        {
            Attr(ControlFactory.CLICK, fun);
        }

        public virtual void Dblclick(string fun)
        {
            Attr(ControlFactory.DBLCLICK, fun);
        }

        public virtual void Focus(string fun)
        {
            Attr(ControlFactory.FOCUS, fun);
        }

        public virtual void Keydown(string fun)
        {
            Attr(ControlFactory.KEYDOWN, fun);
        }

        public virtual void Keypress(string fun)
        {
            Attr(ControlFactory.KEYPRESS, fun);
        }

        public virtual void Keyup(string fun)
        {
            Attr(ControlFactory.KEYUP, fun);
        }

        public virtual void Mousedown(string fn)
        {
            Attr(ControlFactory.MOUSEDOWN, fn);
        }

        public virtual void Mousemove(string fn)
        {
            Attr(ControlFactory.MOUSEMOVE, fn);
        }

        public virtual void Mouseout(string fn)
        {
            Attr(ControlFactory.MOUSEOUT, fn);
        }

        public virtual void Mouseover(string fn)
        {
            Attr(ControlFactory.MOUSEOVER, fn);
        }

        public virtual void Mouseup(string fn)
        {
            Attr(ControlFactory.MOUSEUP, fn);
        }

        public virtual void Select(string fn)
        {
            Attr(ControlFactory.SELECT, fn);
        }

        public virtual void Submit(string fun)
        {
            Attr(ControlFactory.SUBMIT, fun);
        }




        public virtual void FindControl(string key)
        {
            Value = webControl.FindControl(key);
        }

        public virtual void Show()
        {
            webControl.Visible = true;
        }

        public virtual void Hide()
        {
            webControl.Visible = false;
        }

        public virtual void Disabled()
        {
            webControl.Enabled = false;
        }

        public virtual void Enabled()
        {
            webControl.Enabled = true;
        }

        public virtual void Focus()
        {
            webControl.Focus();
        }

        public virtual void Html()
        {
        }

        public virtual void Html(string value)
        {
        }

        public virtual void Html(Func<string> fun)
        {
        }

        public virtual void Text()
        {
        }

        public virtual void Text(string value)
        {
        }

        public virtual void Text(Func<string> fun)
        {
        }

        #endregion
    }
}
